package com.wanxi.controller;

import com.wanxi.model.StudentModel;

import java.util.ArrayList;

public class MyArrayAddIndex {
    public static void main(String[] args) {
        String[] strings = {"张三,24", "李四,44", "王五,27"};
        ArrayList<StudentModel> list = setList(strings);
//        添加一个对象到集合的第二个元素
        String str="丽丽,18";
        StudentModel model=setStudentModel(str);
        list.add(1,model);
        System.out.println(list);
        String lucy="露西,19";
        StudentModel lucyModel=setStudentModel(lucy);
//        修改元素
        list.set(2,lucyModel);
        System.out.println(list);
    }

    private static ArrayList<StudentModel> setList(String[] strings) {
        ArrayList<StudentModel> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(setStudentModel(strings[i]));
        }
        return list;
    }

    /**
     * 将得到控制台的字符串解析为学生对象
     * 张三,23
     *
     * @param str
     * @return
     */
    private static StudentModel setStudentModel(String str) {
        StudentModel model = new StudentModel();
        model.setName(str.split(",")[0]);
        String ageStr = str.split(",")[1];
        int age = Integer.parseInt(ageStr);
        model.setAge(age);
        return model;
    }
}
